﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrmAuto;

public partial class Controls_RicercheControl : System.Web.UI.UserControl
{
  QuerierLight  mQueri = null;

		public string UrlPaginaRisultati { get; set; }

		protected void Page_Load(object sender, EventArgs e)
		{
				if(!IsPostBack)
				 Inizializza();		 
		}//fM

		protected override void OnPreRender(EventArgs e)
		{
				UrlPaginaRisultati = ResolveUrl(UrlPaginaRisultati) ;
				this.myImageButtonSearch.PostBackUrl = UrlPaginaRisultati ; 
				base.OnPreRender(e);
		}

		private void Inizializza()
		{
		  mQueri = new QuerierLight();
		  
			 this.myDropMarche.DataTextField = "Marca";
				this.myDropMarche.DataValueField = "ID";
				this.myDropMarche.DataSource = mQueri.getMarche();
				this.myDropMarche.DataBind();
				
				this.myDropPossessi.DataTextField = "Possesso" ;
				this.myDropPossessi.DataValueField = "ID";
				this.myDropPossessi.DataSource = mQueri.getPossessi();
				this.myDropPossessi.DataBind();

				this.myDropColori.DataTextField = "Colore";
				this.myDropColori.DataValueField = "ID";
				this.myDropColori.DataSource = mQueri.getColori();
				this.myDropColori.DataBind();
                            
                this.myStatiDrop.DataTextField="STATO_Stato";
                this.myStatiDrop.DataValueField = "STATO_ID";
                this.myStatiDrop.DataSource = mQueri.getStati();
                this.myStatiDrop.DataBind();
                var item = this.myStatiDrop.Items.FindByText("ITALIA (ITALY)");
                 if(item != null)   item.Selected = true;    

                this.myTipiDrop.DataTextField = "TipoIT";
                this.myTipiDrop.DataValueField = "ID";
                this.myTipiDrop.DataSource = mQueri.getTipiAuto();
                this.myTipiDrop.DataBind();
                this.myTipiDrop.Items.FindByText("Auto/Car").Selected = true;   
			
		}//fM


		protected void myDropMarche_SelectedIndexChanged(object sender, EventArgs e)
		{
				mQueri = new QuerierLight();
				var valore = ((DropDownList)sender).SelectedValue;
				if (string.IsNullOrEmpty(valore)) return;
				int idMarca = 0;
				if (!int.TryParse(valore, out idMarca)) return;

				this.myDropModelli.DataTextField = "Modello";
				this.myDropModelli.DataValueField = "ID";
				this.myDropModelli.Items.Clear();

				this.myDropModelli.Items.Add(
						new ListItem("Qualsiasi / All", "0", true));
				this.myDropModelli.DataSource = mQueri.getModelli(idMarca);
				    
				this.myDropModelli.DataBind();
				
		}//fM



}//fC
